package com.mike.bdb.ui;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import com.mike.bdb.util.DataCurrent;
import com.mike.bdb.util.ImageManager;
import com.mike.swt.util.ShellUtil;

public class HistoryUpdate {

	private Shell motherland;
	protected Shell shell;

	private volatile boolean open = false;
	private StyledText styledText;
	
	public HistoryUpdate(Shell motherland) {
		this.motherland = motherland;
	}
	
	public boolean isOpen() {
		return open;
	}

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			HistoryUpdate window = new HistoryUpdate(null);
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		open = true;
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		open = false;
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell(SWT.CLOSE | SWT.BORDER | SWT.APPLICATION_MODAL);
		shell.setImage(ImageManager.getImage(ImageManager.ICON_NEW));
		shell.setText(Messages.getString("HistoryUpdate.title"));
		shell.setSize(450, 400);
		shell.setLayout(new FillLayout(SWT.HORIZONTAL));
		{
			{
				{
					Composite composite = new Composite(shell, SWT.NONE);
					composite.setLayout(new FormLayout());
					{
						ScrolledComposite scrolledComposite = new ScrolledComposite(composite, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
						{
							FormData formData = new FormData();
							formData.left = new FormAttachment(0, 10);
							formData.right = new FormAttachment(100, -10);
							formData.top = new FormAttachment(0, 10);
							formData.bottom = new FormAttachment(100, -10);
							scrolledComposite.setLayoutData(formData);
						}
						scrolledComposite.setExpandHorizontal(true);
						scrolledComposite.setExpandVertical(true);
						{
							styledText = new StyledText(scrolledComposite, SWT.NONE);
							styledText.setEnabled(false);
							styledText.setEditable(false);
							
							styledText.append("\n");
							String current = " Current Version " + DataCurrent.VERSION + "\n";
							styledText.append(current);
							styledText.append("\r\n");
							StyleRange style = new StyleRange();
						    style.start = 0;
						    style.length = current.length();
						    style.fontStyle = SWT.BOLD;
						    style.foreground = Display.getDefault().getSystemColor(SWT.COLOR_DARK_GREEN);
						    styledText.setStyleRange(style);
							styledText.append(DataCurrent.getVersionHistory());
						}
						scrolledComposite.setContent(styledText);
						scrolledComposite.setMinSize(styledText.computeSize(SWT.DEFAULT, SWT.DEFAULT));
					}
				}
			}
		}
		if (null != motherland) {
			ShellUtil.setCenter(shell, motherland);
		}
	}
}
